<?php

/**
  * register actions.
  *
  * @package    beamr
  * @subpackage register
  * @author     Your name here
  * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
  */
class registerActions extends sfActions
{
  /**
    * Executes index action
    *
    * @param sfRequest $request A request object
    */
  public function executeIndex(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated())
    {
      $this->redirect('@user_edit');
    }

    $this->form = new RegisterForm();
    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('sf_guard_user'));
      if ($this->form->isValid())
      {
        $this->form->save();

        $this->getUser()->signIn($this->form->getObject());
        $this->redirect('@presentation');
      }
    }
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($this->getUser()->isAuthenticated(), 'You must me logged-in to edit your account.');
    $this->form = new RegisterForm($this->getUser()->getGuardUser());

    if ($request->isMethod('post') || $request->isMethod('put'))
    {
      $this->form->bind($request->getParameter('sf_guard_user'));
      if ($this->form->isValid())
      {
        $this->form->save();
        $this->getUser()->setFlash('success', 'All changes has been saved.');
      }
    }
    $this->setTemplate('index');
  }

}
